/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web.share;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.share.RuleType;
import com.boxin.ims.modules.wechat.entity.share.ShareRule;
import com.boxin.ims.modules.wechat.service.share.ShareRuleService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 分享转发中奖规则Controller
 * @author Jakemanse
 * @version 2013-10-01
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/share/shareRule")
public class ShareRuleController extends BaseController {

	@Autowired
	private ShareRuleService shareRuleService;
	
	@ModelAttribute
	public ShareRule get(@RequestParam(required=false) Long id) {
		if (id != null){
			return shareRuleService.get(id);
		}else{
			return new ShareRule();
		}
	}
	
	@RequiresPermissions("wechat:share:shareRule:view")
	@RequestMapping(value = {"list", ""})
	public String list(ShareRule shareRule, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			shareRule.setUser(user);
		}
        Page<ShareRule> page = shareRuleService.find(new Page<ShareRule>(request, response), shareRule); 
        model.addAttribute("page", page);
		return "modules/wechat/share/shareRuleList";
	}

	@RequiresPermissions("wechat:share:shareRule:view")
	@RequestMapping(value = "form")
	public String form(ShareRule shareRule, Model model) {
		User user = UserUtils.getUser();
		shareRule.setUser(user);
		model.addAttribute("shareRule", shareRule);
		
		List<RuleType> ruleTypeList = RuleType.ruleMapList;
		model.addAttribute("ruleTypeList", ruleTypeList);
		
		return "modules/wechat/share/shareRuleForm";
	}

	@RequiresPermissions("wechat:share:shareRule:edit")
	@RequestMapping(value = "save")
	public String save(ShareRule shareRule, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, shareRule)){
			return form(shareRule, model);
		}
		shareRuleService.save(shareRule);
		addMessage(redirectAttributes, "保存分享转发中奖规则'" + shareRule.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/share/shareRule/?repage";
	}
	
	@RequiresPermissions("wechat:share:shareRule:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		shareRuleService.delete(id);
		addMessage(redirectAttributes, "删除分享转发中奖规则成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/share/shareRule/?repage";
	}

}
